package org.chen.controller;
import lombok.extern.slf4j.Slf4j;
import org.chen.model.Article;
import org.chen.model.ImageInfo;
import org.chen.model.Section;
import org.chen.model.dto.*;
import org.chen.model.elastic.AdminArticleQueryParam;
import org.chen.model.elastic.ArticlePageResult;
import org.chen.model.mongodb.ArticleDocument;
import org.chen.model.vo.ArticlePreviewVO;
import org.chen.model.vo.ArticleVO;
import org.chen.response.Result;
import org.chen.service.impl.AdminArticleServiceImpl;
import org.chen.service.impl.ArticleSortService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/articles")
@Validated
@Slf4j
public class ArticleController {

    private final AdminArticleServiceImpl adminArticleService;
    private final ArticleSortService sortService;

    @Autowired
    private MongoTemplate mongoTemplate;

    public ArticleController(AdminArticleServiceImpl articleService,
                             ArticleSortService sortService) {
        this.adminArticleService = articleService;
        this.sortService = sortService;
    }

    @GetMapping
    public Result<ArticlePageResult> getArticleList(AdminArticleQueryParam queryParam) {
        ArticlePageResult result = adminArticleService.getArticleList(queryParam);
        return Result.success(result);
    }

    @GetMapping("/drafts")
    public Result<ArticlePageResult> getDraftsArticleList(AdminArticleQueryParam queryParam) {
        ArticlePageResult result = adminArticleService.getDraftsArticleList(queryParam);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result<ArticleVO> getArticle(@PathVariable String id) {
        ArticleVO article = adminArticleService.getArticleById(id);
        return Result.success(article);
    }

    @GetMapping("/edit/{id}")
    public Result<ArticleVO> getArticleEdit(@PathVariable String id) {
        ArticleVO article = adminArticleService.getArticleByIdForEdit(id);
        return Result.success(article);
    }

    @PostMapping
    public Result<String> createArticle(@RequestBody @Validated ArticleDTO articleDTO) {
        String id = adminArticleService.createArticle(articleDTO);
        return Result.success(id);
    }

    @PutMapping("/{id}")
    public Result<Void> updateArticle(@PathVariable String id,
                                      @RequestBody @Validated ArticleUpdateDTO articleDTO) {
        adminArticleService.updateArticle(id, articleDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteArticle(@PathVariable String id) {
        adminArticleService.deleteArticle(id);
        return Result.success();
    }

    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable String id, @RequestBody Map<String,Integer> status) {
        adminArticleService.updateStatus(id, status.get("status"));
        return Result.success();
    }

    @PutMapping("/{id}/top")
    public Result updateTopStatus(@PathVariable String id,
                                  @RequestParam Boolean isTop,
                                  @RequestParam(required = false) Integer topOrder) {
        sortService.updateTopStatus(id, isTop, topOrder);
        return Result.success();
    }

    @PutMapping("/{id}/hot")
    public Result updateHotStatus(@PathVariable String id,
                                  @RequestParam Boolean isHot,
                                  @RequestParam(required = false) Integer hotOrder) {
        sortService.updateHotStatus(id, isHot, hotOrder);
        return Result.success();
    }

    @PutMapping("/{id}/recommend")
    public Result updateRecommendStatus(@PathVariable String id,
                                        @RequestParam Boolean isRecommend,
                                        @RequestParam(required = false) Integer recommendOrder) {
        sortService.updateRecommendStatus(id, isRecommend, recommendOrder);
        return Result.success();
    }

    @PutMapping("/batch/source")
    public Result<Void> updateArticlesSource(@RequestBody @Validated ArticleBatchSourceDTO dto) {
        log.info("Batch update articles source: {}", dto);
        adminArticleService.updateArticlesSource(dto.getIds(), dto.getSource());
        return Result.success();
    }

    @PutMapping("/batch/status")
    public Result<Void> updateArticlesStatus(@RequestBody @Validated ArticleBatchStatusDTO dto) {
        log.info("Batch update articles status: {}", dto);
        adminArticleService.updateArticlesStatus(dto.getIds(), dto.getStatus());
        return Result.success();
    }

    @DeleteMapping("/batch")
    public Result<Void> deleteArticles(@RequestBody @Validated ArticleBatchIdsDTO dto) {
        log.info("Batch delete articles: {}", dto);
        adminArticleService.deleteArticles(dto.getIds());
        return Result.success();
    }

    // 批量更新置顶状态
    @PutMapping("/batch/top")
    public Result<Void> batchUpdateTop(@RequestBody @Validated ArticleBatchSortDTO dto) {
        log.info("Batch update articles top status: {}", dto);
        sortService.batchUpdateTopStatus(dto.getIds(), dto.getIsTop(), dto.getTopOrder());
        return Result.success();
    }

    // 批量更新热门状态
    @PutMapping("/batch/hot")
    public Result<Void> batchUpdateHot(@RequestBody @Validated ArticleBatchSortDTO dto) {
        log.info("Batch update articles hot status: {}", dto);
        sortService.batchUpdateHotStatus(dto.getIds(), dto.getIsHot(), dto.getHotOrder());
        return Result.success();
    }

    // 批量更新推荐状态
    @PutMapping("/batch/recommend")
    public Result<Void> batchUpdateRecommend(@RequestBody @Validated ArticleBatchSortDTO dto) {
        log.info("Batch update articles recommend status: {}", dto);
        sortService.batchUpdateRecommendStatus(dto.getIds(), dto.getIsRecommend(), dto.getRecommendOrder());
        return Result.success();
    }

    @GetMapping("/preview/{id}")
    public Result<ArticlePreviewVO> previewArticle(@PathVariable String id) {
        ArticleDocument article = mongoTemplate.findById(id, ArticleDocument.class);
        if (article == null) {
            return Result.failed("文章不存在");
        }

        ArticlePreviewVO previewVO = new ArticlePreviewVO();
        BeanUtils.copyProperties(article, previewVO);

        return Result.success(previewVO);
    }
}
